﻿using UnityEngine;
using System.Collections;

public class LayerManager : MonoBehaviour
{
    public static void AddLayer(Camera camera, string layerName)
    {
        int layer = LayerMask.NameToLayer(layerName);
        camera.cullingMask = camera.cullingMask | (1 << layer);
    }

    public static void RemoveLayer(Camera camera, string layerName)
    {
        int layer = LayerMask.NameToLayer(layerName);
        camera.cullingMask = camera.cullingMask & (~(1 << layer));
    }

    public static void AddAllLayer(Camera camera)
    {
        camera.cullingMask = -1;
    }

    public static void RemoveAllLayer(Camera camera)
    {
        camera.cullingMask = 0;
    }
	
	public static void SetLayer(Camera ca, string layerName)
	{
		RemoveAllLayer(ca);
		AddLayer(ca, layerName);
	}
	
	public static void SetLayer(GameObject go, string layerName, bool children)
	{
		int layer = LayerMask.NameToLayer(layerName);
		SetLayer(go.transform, layer, children);
	}
	
	static void SetLayer(Transform trans, int layer, bool children)
	{		
		trans.gameObject.layer = layer;		
		if(children)
		{
			for(int i = 0; i < trans.childCount; i++)
				SetLayer(trans.GetChild(i), layer, children);
		}
	}
}
